<?php


namespace control_function;


class control
{
    protected $control_par;
    protected $user_id;
    protected $start_date;
    protected $end_date;
    protected $ModuleContent = array(
        array(
            "26e8f516-ae59-448d-8747-826d3251a1f2",
            "fab fa-modx"
        ),
        array(
            "a48062e9-7e30-48e7-b7bd-ad4aca41a86d",
            "fas fa-hand-holding-usd"
        ),
        array(
            "jifen",
            "fas fa-hand-holding-usd"
        ),
        array(
            "75e2e7c7-b1dc-4ef5-a6bd-f8787d2978b1",
            "fas fa-play-circle"
        ),
        array(
            "100c7db7-e23f-4631-84ea-c79c313125d6",
            "far fa-calendar-alt"
        ),
        array(
            "39ca5c96-477f-4c59-88a2-8d3d3e288b7c",
            "fas fa-align-justify"
        ),
        array(
            "461eb79f-014f-4f13-b9ef-53f9146205e9",
            "far fa-list-alt"
        ),
        array(
            "dd6b7887-2276-47bc-9e16-eb62d1132a61",
            "fas fa-file-signature"
        )
    );

    public function get_par(array $par)
    {
        $this->control_par = $par;
    }

    public function get_user_id(string $par_id)
    {
        $this->user_id = $par_id;
    }

    public function get_start_date($par_date)
    {
        $this->start_date = $par_date;
    }

    public function get_end_date($par_date)
    {
        $this->end_date = $par_date;
    }
}

class SideBarControl extends control
{
    protected function find_menu_array($db,$value,array $ary){
        $menu_array = array();
        $from = $db->query("SELECT ModuleId,ParentId,FullName from Base_Module where ModuleId = '$value'");
        $row = $from->fetch();
        if($row['ParentId'] == 0){
            $menu_array[$row['ModuleId']] = $ary;
            $menu_array[$row['ModuleId']]['name'] = $row['FullName'];
            $result[0] = $row['ModuleId'];
            $result[1] = $menu_array;
            return $result;
        }
        else{
            $ary[$row['ModuleId']]['name'] = $row['FullName'];
            $ary[$row['ParentId']][$row['ModuleId']] = $ary[$row['ModuleId']];
            $this->find_menu_array($db,$row['ParentId'],$ary);
        }
    }

    public function PutDownLi($db)
    {
        //开始整理用户关联到的items，将所有关联items归类为一个数组的树状结构
        $items_all = $this->control_par;
        $ParentArray = array();
        $parent_temp = array();
        foreach ($items_all as $value) {
            $from = $db->query("SELECT ModuleId,ParentId,FullName,Target from Base_Module where ModuleId = '$value'");
            $row = $from->fetch();
            if(strcmp($row['Target'],"expand") == 0){//数据库中，目录类为expand
                $parent_temp[$row['ModuleId']]['name'] = $row['FullName'];
            }
            $parent_temp[$row['ParentId']][$row['ModuleId']]['name'] = $row['FullName'];//根据从属关系，首次归类为第一个数组
        }
        $ParentArray = $parent_temp['0'];//将第一个数组中最外层，即parentID = 0的数组传递给parentarray
        //依据parentarray，从外向内第一次归类第二层数组
        foreach ($ParentArray as $key => $value){
            foreach ($parent_temp as $key_1 => $value_1){
                if(strcmp($key_1,$key) == 0){
                    $ParentArray[$key] = array_merge($ParentArray[$key],$parent_temp[$key_1]);
                }
            }
        }
        //归类第三层数组
        reset($ParentArray);
        foreach ($ParentArray as $key => $value){
            foreach ($value as $key_2 => $value_2){
                foreach ($parent_temp as $key_1 => $value_1){
                    if(strcmp($key_1,$key_2) == 0){
                        $ParentArray[$key][$key_2] = array_merge($ParentArray[$key][$key_2],$parent_temp[$key_1]);
                    }
                }
            }
        }
        //这个方法很蠢，只能归类三层数组，无法拓展到无限层
        $li_before = "<li class='sidebar_li'><span style='font-size: 32px;'><i class='";
        $li_middle = "'></i></span><div class='sidebar_li_text'>";
        $li_after = "</div>";
        $li_content_before = "<ul>";
        $li_content_after = "</ul>";
        $li_end = "</li>";
        $li_li_before = "<li class='sidebar_li'><a class='li_button' href='#";
        $li_li_before_middle = "' ><span><i class='";
        $li_li_middle = "'></i></span>";
        $li_li_after = "</a>";
        if ($ParentArray != null) {
            foreach ($ParentArray as $key => $value) {
                foreach ($this->ModuleContent as $value_in) {
                    if ($key == $value_in[0]) {
                        $ParentArray[$key]['icon'] = $value_in[1];
                    }
                }
                foreach ($value as $key_in => $value_in) {
                    foreach ($this->ModuleContent as $value_in_in) {
                        if ($key_in == $value_in_in[0]) {
                            $ParentArray[$key][$key_in]['icon'] = $value_in_in[1];
                        }
                    }
                }
            }
            reset($ParentArray);
            echo '<!--';
            print_r($ParentArray);
            echo '-->';
            //积分商场不在数据库内，需要单独定义
            $ParentArray['a48062e9-7e30-48e7-b7bd-ad4aca41a86d']['jifen'] = array('name'=>'积分商城','icon'=>'fas fa-hand-holding-usd');
            foreach ($ParentArray as $key => $value) {
                echo $li_before . $ParentArray[$key]['icon'] . $li_middle . $ParentArray[$key]['name'] . $li_after;
                echo $li_content_before;
                foreach ($value as $key_in => $value_in) {
                    if (strcmp($key_in, "name") != 0 && strcmp($key_in, "icon") != 0) {
                        echo $li_li_before . $key_in . $li_li_before_middle . $value_in['icon'] . $li_li_middle . $value_in['name'] . $li_li_after . $li_end;
                    }
                }
                echo $li_content_after . $li_end;
            }
        }

    }

}

class ContentControl extends control
{
    public function MySuggest_ShowSearchResult($db, $words)
    {
        echo "<table><tr><th>序号</th><th>当前状态</th><th>提议人</th><th>SN</th><th>提议名称</th><th>提议类型</th><th>现状</th><th>措施</th><th>提议日期</th></tr>";
        $num = 1;
        $from = $db->query("SELECT State,AddUserName,SN,S_Title,S_Type,CurrentDesc,ImproveMethod,AddTime from T_Suggest where AddUserId = '$this->user_id' AND Delete_flag = '0' ORDER BY AddTime DESC");
        while ($row = $from->fetch()) {
            $yes_in = false;
            if ($words == null) {
                $yes_in = true;
            } else {
                foreach ($row as $key => $value) {
                    if (strstr($value, $words) && $key != 'SN' && $key != 'AddTime' && !is_int($key)) {
                        $yes_in = true;
                    }
                }
            }
            if ($yes_in) {
                //echo "<tr><td>" . $num . "</td><td>" . $row['State'] . "</td><td>" . $row['AddUserName'] . "</td><td>" . $row['SN'] . "</td><td>" . $row['S_Title'] . "</td><td>" . $row['S_Type'] . "</td><td>" . $row['CurrentDesc'] . "</td><td>" . $row['ImproveMethod'] . "</td><td>" . $row['AddTime'] . "</td></tr>";
                echo "<tr class='sug_content_tr'><td class='num'>" . $num . "</td>";
                foreach ($row as $key => $value){
                    if(!is_int($key)){
                        echo "<td class='".$key."'>".$value."</td>";
                    }
                }
                echo "</tr>";
                $num++;
            }
        }
        echo "</table>";
    }

    public function MyImpl_ShowSearchResult($db, $words)
    {
        $from = $db->query("SELECT RealName from Base_User where Account = '$this->user_id'");
        $row = $from->fetch();
        $name = $row['RealName'];
        echo "<table><tr><th>序号</th><th>实施人</th><th>当前状态</th><th>SN</th><th>提议名称</th><th>提议类型</th><th>现状</th><th>措施</th><th>实施日期</th></tr>";
        $num = 1;
        $from = $db->query("SELECT State,SN,S_Title,S_Type,CurrentDesc,ImproveMethod,ImplComfirmTime from T_Suggest where ImplUserId = '$this->user_id' AND Delete_flag = '0' ORDER BY ImplComfirmTime DESC");
        while ($row = $from->fetch()) {
            $yes_in = false;
            if ($words == null) {
                $yes_in = true;
            } else {
                foreach ($row as $key => $value) {
                    if (strstr($value, $words) && $key != 'ImplComfirmTime' && !is_int($key)) {
                        $yes_in = true;
                    }
                }
            }
            if ($yes_in) {
                if(strcmp($row['State'],'建议指派通过') == 0 || strcmp($row['State'],'建议实施通过') == 0){
                    echo "<tr class='sug_content_tr'><td class='num'>" . $num . "</td><td class='realname'>" . $name . "</td>";
                    foreach ($row as $key => $value){
                        if(!is_int($key)){
                            echo "<td class='".$key."'>".$value."</td>";
                        }
                    }
                    echo "</tr>";
                    $num++;
                }
            }
        }
        echo "</table>";
    }

    public function CI_ShowSearchResult($db, $words)
    {
        $from = $db->query("SELECT RealName from Base_User where Account = '$this->user_id'");
        $row = $from->fetch();
        $name = $row['RealName'];
        echo "<table><tr><th>序号</th><th>CI评审</th><th>当前状态</th><th>SN</th><th>提议名称</th><th>提议类型</th><th>现状</th><th>措施</th><th>提议日期</th></tr>";
        $num = 1;
        $from = $db->query("SELECT State,SN,S_Title,S_Type,CurrentDesc,ImproveMethod,AddTime from T_Suggest where CI_EngineerId = '$this->user_id' AND State = '建议发布' AND Delete_flag = '0' ORDER BY AddTime DESC");
        while ($row = $from->fetch()) {
            $yes_in = false;
            if ($words == null) {
                $yes_in = true;
            } else {
                foreach ($row as $key => $value) {
                    if (strstr($value, $words) && $key != 'AddTime' && !is_int($key)) {
                        $yes_in = true;
                    }
                }
            }
            if ($yes_in) {
                //echo "<tr><td>" . $num . "</td><td>" . $row['State'] . "</td><td>" . $name . "</td><td>". $row['SN'] . "</td><td>" . $row['S_Title'] . "</td><td>" . $row['S_Type'] . "</td><td>" . $row['CurrentDesc'] . "</td><td>" . $row['ImproveMethod'] . "</td><td>" . $row['AddTime'] . "</td></tr>";
                echo "<tr class='sug_content_tr'><td class='num'>" . $num . "</td><td class='realname'>" . $name . "</td>";
                foreach ($row as $key => $value){
                    if(!is_int($key)){
                        echo "<td class='".$key."'>".$value."</td>";
                    }
                }
                echo "</tr>";
                $num++;
            }
        }
        echo "</table>";
    }

    public function ShowAll($db, $words, $state, $dept)
    {
        echo "<table><tr><th>序号</th><th>当前状态</th><th>提议人</th><th>SN</th><th>提议名称</th><th>提议类型</th><th>现状</th><th>措施</th><th>提议日期</th></tr>";
        $num = 1;
        $sql_start = "SELECT State,AddUserName,SN,S_Title,S_Type,CurrentDesc,ImproveMethod,AddTime from T_Suggest where Delete_flag = '0' ";
        if($this->start_date != null && $this->end_date != null){
            $this->start_date = $this->start_date . " 00:00:00.000";
            $this->end_date = $this->end_date . " 23:59:59.999";
            $sql_start = $sql_start . "AND AddTime BETWEEN '$this->start_date' AND '$this->end_date' ";
        }
        if ($state != null) {
            $sql_start = $sql_start . "AND State = '$state' ";
        }
        if ($dept != null) {
            $sql_start = $sql_start . "AND AddUserDept = '$dept' ";
        }
        $sql_start = $sql_start . "ORDER BY AddTime DESC";
        $from = $db->query($sql_start);
        while ($row = $from->fetch()) {
            $yes_in = false;
            if ($words == null) {
                $yes_in = true;
            } else {
                foreach ($row as $key => $value) {
                    if (strstr($value, $words) && $key != 'AddTime' && !is_int($key)) {
                        $yes_in = true;
                    }
                }
                reset($row);
            }
            if ($yes_in) {
                //echo "<tr><td class='num'>" . $num . "</td><td class='state'>" . $row['State'] . "</td><td>" . $row['AddUserName'] . "</td><td>" . $row['SN'] . "</td><td>" . $row['S_Title'] . "</td><td>" . $row['S_Type'] . "</td><td>" . $row['CurrentDesc'] . "</td><td>" . $row['ImproveMethod'] . "</td><td>" . $row['AddTime'] . "</td></tr>";
                echo "<tr class='sug_content_tr' ><td class='num'>" . $num . "</td>";
                foreach ($row as $key => $value){
                    if(!is_int($key)){
                        echo "<td class='".$key."'>".$value."</td>";
                    }
                }
                echo "</tr>";
                $num++;
            }
        }
        echo "</table>";
    }

    public function Auditor_ShowSearchResult($db, $words)
    {
        $from = $db->query("SELECT RealName from Base_User where Account = '$this->user_id'");
        $row = $from->fetch();
        $name = $row['RealName'];
        echo "<table><tr><th>序号</th><th>评审员</th><th>当前状态</th><th>SN</th><th>提议名称</th><th>提议类型</th><th>现状</th><th>措施</th><th>提议日期</th></tr>";
        $num = 1;
        $from = $db->query("SELECT State,SN,S_Title,S_Type,CurrentDesc,ImproveMethod,AddTime from T_Suggest where AcceptUserId = '$this->user_id' AND State in ('建议审核通过','建议完成') AND Delete_flag = '0' ORDER BY AddTime DESC");
        while ($row = $from->fetch()) {
            $yes_in = false;
            if ($words == null) {
                $yes_in = true;
            } else {
                foreach ($row as $key => $value) {
                    if (strstr($value, $words) && $key != 'AddTime' && !is_int($key)) {
                        $yes_in = true;
                    }
                }
            }
            if ($yes_in) {
                echo "<tr class='sug_content_tr'><td class='num'>" . $num . "</td><td class='realname'>" . $name . "</td>";
                foreach ($row as $key => $value){
                    if(!is_int($key)){
                        echo "<td class='".$key."'>".$value."</td>";
                    }
                }
                echo "</tr>";
                $num++;
            }
        }
        echo "</table>";
    }
}